package drds.binlog.parse.driver.socket;

import org.apache.commons.lang.StringUtils;

import java.net.SocketAddress;


public abstract class SocketChannels
{

    public static SocketChannel open(SocketAddress socketAddress) throws Exception
    {
        String type = chooseSocketChannel();
        if ("netty".equalsIgnoreCase(type))
        {
            return NettySocketChannels.open(socketAddress);
        } else
        {
            return BioSocketChannels.open(socketAddress);
        }

    }

    private static String chooseSocketChannel()
    {
        String socketChannel = System.getenv("canal.socketChannel");
        if (StringUtils.isEmpty(socketChannel))
        {
            socketChannel = System.getProperty("canal.socketChannel");
        }

        if (StringUtils.isEmpty(socketChannel))
        {
            socketChannel = "bio"; // bio or netty
        }

        return socketChannel;
    }
}
